package cp4.test;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/cp4/data/context1")
public class ServletContextDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext ctx = req.getServletContext();// 获取到整个项目的servlet容器上下文
        Object count = ctx.getAttribute("page_visit_count");
        if(count == null){
            count = 1;
        }else{
            count = (int)count + 1;
        }
        ctx.setAttribute("page_visit_count",count);
        resp.getWriter().println("u visit site total " + count);
    }
}
